/*
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package au.com.domlogic.pvc.xhtml.servlet;

import au.com.domlogic.pvc.core.servlet.LocalRedirectionResponseServlet;
import au.com.domlogic.pvc.core.servlet.LocalRedirectionResponseWrapper;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Domenic Figliomeni
 */
public class XHTMLStrictServlet extends LocalRedirectionResponseServlet {

    @Override
    protected void doPut(HttpServletRequest request, LocalRedirectionResponseWrapper response) throws ServletException, IOException {
        if (request instanceof XHTMLStrictRequestWrapper) {
            XHTMLStrictRequestWrapper wrapper = (XHTMLStrictRequestWrapper) request;
            doPut(wrapper, response);
        } else {
            super.doPut(request, response);
        }
    }

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected final void doGet(HttpServletRequest request, LocalRedirectionResponseWrapper response)
            throws ServletException, IOException {
        if (request instanceof XHTMLStrictRequestWrapper) {
            XHTMLStrictRequestWrapper wrapper = (XHTMLStrictRequestWrapper) request;
            doGet(wrapper, response);
        } else {
            super.doGet(request, response);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected final void doPost(HttpServletRequest request, LocalRedirectionResponseWrapper response)
            throws ServletException, IOException {
        if (request instanceof XHTMLStrictRequestWrapper) {
            XHTMLStrictRequestWrapper wrapper = (XHTMLStrictRequestWrapper) request;
            doPost(wrapper, response);
        } else {
            super.doPost(request, response);
        }
    }

    @Override
    protected void doDelete(HttpServletRequest request, LocalRedirectionResponseWrapper response)
            throws ServletException, IOException {
        if (request instanceof XHTMLStrictRequestWrapper) {
            XHTMLStrictRequestWrapper wrapper = (XHTMLStrictRequestWrapper) request;
            doDelete(wrapper, response);
        } else {
            super.doDelete(request, response);
        }
    }

    @Override
    protected void doHead(HttpServletRequest request, LocalRedirectionResponseWrapper response)
            throws ServletException, IOException {
        if (request instanceof XHTMLStrictRequestWrapper) {
            XHTMLStrictRequestWrapper wrapper = (XHTMLStrictRequestWrapper) request;
            doHead(wrapper, response);
        } else {
            super.doHead(request, response);
        }
    }

    @Override
    protected void doOptions(HttpServletRequest request, LocalRedirectionResponseWrapper response)
            throws ServletException, IOException {
        if (request instanceof XHTMLStrictRequestWrapper) {
            XHTMLStrictRequestWrapper wrapper = (XHTMLStrictRequestWrapper) request;
            doOptions(wrapper, response);
        } else {
            super.doOptions(request, response);
        }
    }

    @Override
    protected void doTrace(HttpServletRequest request, LocalRedirectionResponseWrapper response)
            throws ServletException, IOException {
        if (request instanceof XHTMLStrictRequestWrapper) {
            XHTMLStrictRequestWrapper wrapper = (XHTMLStrictRequestWrapper) request;
            doTrace(wrapper, response);
        } else {
            super.doTrace(request, response);
        }
    }

    @Override
    protected long getLastModified(HttpServletRequest request) {
        long result = 0;
        if (request instanceof XHTMLStrictRequestWrapper) {
            XHTMLStrictRequestWrapper wrapper = (XHTMLStrictRequestWrapper) request;
            result = getLastModified(wrapper);
        } else {
            result = super.getLastModified(request);
        }
        return result;
    }

    /*
     * The following methods are added to spare the developers of extending servets
     * the burden of casting the request allowing them to focus on view logic.
     *
     * TODO: Have a look at what the service methods are doing, maybe I should
     * add a service method with the wrapper in the signature
     */
    protected void doPut(XHTMLStrictRequestWrapper request, LocalRedirectionResponseWrapper response)
            throws ServletException, IOException {
        super.doPut(request, response);

    }

    protected void doGet(XHTMLStrictRequestWrapper request, LocalRedirectionResponseWrapper response)
            throws ServletException, IOException {
        super.doGet(request, response);
    }

    protected void doPost(XHTMLStrictRequestWrapper request, LocalRedirectionResponseWrapper response)
            throws ServletException, IOException {
        super.doPost(request, response);
    }

    protected void doDelete(XHTMLStrictRequestWrapper request, LocalRedirectionResponseWrapper response)
            throws ServletException, IOException {
        super.doDelete(request, response);
    }

    protected void doHead(XHTMLStrictRequestWrapper request, LocalRedirectionResponseWrapper response)
            throws ServletException, IOException {
        super.doHead(request, response);
    }

    protected void doOptions(XHTMLStrictRequestWrapper request, LocalRedirectionResponseWrapper response)
            throws ServletException, IOException {
        super.doOptions(request, response);
    }

    protected void doTrace(XHTMLStrictRequestWrapper request, LocalRedirectionResponseWrapper response)
            throws ServletException, IOException {
        super.doTrace(request, response);
    }

    protected long getLastModified(XHTMLStrictRequestWrapper request) {
        return super.getLastModified(request);
    }
}
